package com.enjoy.studyfragment.fragment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.enjoy.mjbaseframe.util.GlideUtil;
import com.enjoy.studyfragment.R;
import com.enjoy.studyfragment.bean.ContactSelectBean;
import com.enjoy.studyfragment.widget.FloatingGroupExpandableListView;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

public class PinnedHeaderExpandableAdapter extends BaseExpandableListAdapter implements ExpandableListView.OnChildClickListener {

    private Context context;
    private LayoutInflater inflater;
    private FloatingGroupExpandableListView listView;
    private List<ContactSelectBean> list = new ArrayList<>();

    public PinnedHeaderExpandableAdapter(Context context, FloatingGroupExpandableListView listView) {
        this.context = context;
        this.listView = listView;
        inflater = LayoutInflater.from(this.context);
    }

    public void setData(List<ContactSelectBean> list){
        this.list = list;
    }

    public List<ContactSelectBean> getData(){
        return list;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getPatriarchs().get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        if(list.size() == 0){
            return 0;
        }
        return list.get(groupPosition).getPatriarchs().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition).getClassInfo();
    }

    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        handleClick(childPosition, groupPosition);
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_group, null);
        }
        View paddTop = convertView.findViewById(R.id.paddTop);
        TextView tvCount = (TextView)convertView.findViewById(R.id.tvCount);
        ImageView iv = (ImageView) convertView.findViewById(R.id.ivExpand);
        TextView text = (TextView) convertView.findViewById(R.id.tvGroup);
        CheckBox groupSelect = convertView.findViewById(R.id.groupSelect);
        //第一个item顶部间距隐藏
        if (groupPosition == 0) {
            paddTop.setVisibility(View.GONE);
        } else {
            paddTop.setVisibility(View.VISIBLE);
        }
        //折叠箭头切换
        if (isExpanded) {
            iv.setImageResource(R.mipmap.operation_icon_expand_s);
        } else {
            iv.setImageResource(R.mipmap.operation_icon_expand_n);
        }
        //group名称
        text.setText(list.get(groupPosition).getClassInfo().getClass_name());
        //组员勾选状态切换
        boolean select = list.get(groupPosition).getClassInfo().isSelect();
        groupSelect.setChecked(select);
        groupSelect.setOnClickListener(new Group_CheckBox_Click(groupPosition));
        //分组人数
        tvCount.setText(getChildrenCount(groupPosition)+"人");
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_contact_item, null);
        }
        ContactSelectBean.PatriarchsBean bean = list.get(groupPosition).getPatriarchs().get(childPosition);
        CheckBox select = convertView.findViewById(R.id.select);
        CircleImageView ivAvtar = convertView.findViewById(R.id.ivAvtar);
        TextView text = (TextView) convertView.findViewById(R.id.item_name);
        LinearLayout llSelect = (LinearLayout) convertView.findViewById(R.id.llSelect);
        //组员名称
        text.setText(bean.getUser_name());
        //头像
        GlideUtil.loadImageView(context,bean.getHead_url(),ivAvtar,R.mipmap.my_img_avatar_one);
        //组员勾选状态切换
        boolean childSelect = bean.isSelect();
        select.setChecked(childSelect);
        select.setOnClickListener(new Child_CheckBox_Click(groupPosition, childPosition));
        llSelect.setOnClickListener(new Child_CheckBox_Click(groupPosition, childPosition));
        return convertView;
    }

    /** 勾選 Group CheckBox 時，存 Group CheckBox 的狀態，以及改變 Child CheckBox 的狀態 */
    class Group_CheckBox_Click implements View.OnClickListener {
        private int groupPosition;

        Group_CheckBox_Click(int groupPosition) {
            this.groupPosition = groupPosition;
        }

        public void onClick(View v) {
            list.get(groupPosition).getClassInfo().toggle();

            // 將 Children 的 isChecked 全面設成跟 Group 一樣
            int childrenCount = list.get(groupPosition).getPatriarchs().size();
            boolean groupIsChecked = list.get(groupPosition).getClassInfo().isSelect();
            for (int i = 0; i < childrenCount; i++)
                list.get(groupPosition).getPatriarchs().get(i).setSelect(groupIsChecked);

            // 注意，一定要通知 ExpandableListView 資料已經改變，ExpandableListView 會重新產生畫面
            notifyDataSetChanged();
        }
    }

    /** 勾選 Child CheckBox 時，存 Child CheckBox 的狀態 */
    class Child_CheckBox_Click implements View.OnClickListener {
        private int groupPosition;
        private int childPosition;

        Child_CheckBox_Click(int groupPosition, int childPosition) {
            this.groupPosition = groupPosition;
            this.childPosition = childPosition;
        }

        public void onClick(View v) {
            handleClick(childPosition, groupPosition);
        }
    }

    public void handleClick(int childPosition, int groupPosition) {
        list.get(groupPosition).getPatriarchs().get(childPosition).toggle();

        // 檢查 Child CheckBox 是否有全部勾選，以控制 Group CheckBox
        int childrenCount = list.get(groupPosition).getPatriarchs().size();
        boolean childrenAllIsChecked = true;
        for (int i = 0; i < childrenCount; i++) {
            if (!list.get(groupPosition).getPatriarchs().get(i).isSelect())
                childrenAllIsChecked = false;
        }

        list.get(groupPosition).getClassInfo().setSelect(childrenAllIsChecked);

        // 注意，一定要通知 ExpandableListView 資料已經改變，ExpandableListView 會重新產生畫面
        notifyDataSetChanged();
    }

}
